{% extends "profilebase.html" %}

{% block scripts %}

	<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}profile/css/autoSuggest.css" />
	
	<!--[if IE 6]><link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}profile/css/ie6.css" /><![endif]-->

    <script language="javascript" src="{{ STATIC_URL }}profile/js/jquery-1.7.1.js"></script>
    <script language="javascript" src="{{ STATIC_URL }}profile/js/jquery.autoSuggest.packed.js"></script>

<script>
$(document).ready(function() {
	console.log("test");
	
	var data = {{friends|safe}};
	console.log(data);
	var data = {items: {{ friends|safe }} };
	$("input[id=to]").autoSuggest(data.items, {selectedItemProp: "name", searchObjProps: "name", asHtmlID: "receivers", startText: "Send to ..."});
	
}); 
</script>


<script type="text/javascript">
	function make_blank_subject(obj)
	{
		if (obj.value == "Subject")
		{
			obj.value ="";
			obj.style.color = "#000000"
		}
	}
	function fill_subject(obj)
	{
		if (obj.value == "")
		{
			obj.value ="Subject";
			obj.style.color = "#A6A6A6";
		}
	}
	function make_blank_content(obj)
	{
		if (obj.value == "Start typing your message here")
		{
			obj.value ="";
			obj.style.color = "#000000"
		}
	}
	function fill_content(obj)
	{
		if (obj.value == "")
		{
			obj.value ="Start typing your message here";
			obj.style.color = "#A6A6A6";
		}
	}
	function make_blank_sendto(obj)
	{
		if (obj.value == "Send to...")
		{
			obj.value ="";
			obj.style.color = "#000000"
		}
	}
	function fill_sendto(obj)
	{
		if (obj.value == "")
		{
			obj.value ="Send to...";
			obj.style.color = "#A6A6A6";
		}
	}
</script>

{% endblock %}

{% block sidebar2 %}
<h4><b><a href="/messaging/">Send a new message</a></b></h4>

{% if threads %}
	{% for thread, notification in threads_and_notifications %}
		<a href="/messaging/{{ thread.id }}/">
		{% if notification > 0 %}
			<b>{{ thread.subject }}[{{ notification }}]</b>
		{% else %}
			{{ thread.subject }}
		{% endif %}
		</a><br/>
		Started on {{ thread.date_started }}<small><a href="/messaging/delete/{{ thread.id }}/"> delete</a></small><br/><br/><br/>
	{% endfor %}
{% if more_threads_available %}
<br/>
<li><small><a href="/messaging/viewMoreThreads/{{ threads|length }}/{{ thread_id }}">view more threads</a></small></li>
{% endif %}
{% else %}
	You currently have no message threads
{% endif %}

{% endblock %}



{% block content %}
{% if messages %}
	<div style="font-size:30;"><b>{{ subject }}</b></div>
	|{% for rec in current_thread.receivers.all %}
		<a href="/myprofile/{{ rec.id }}/">{{ rec }}</a> |  
	{% endfor %}
	<br/><br/>
{% else %}
	<div style="font-size:30;"><b>Send a new message</b></div></br>
{% endif %}	

{% if messages %}
	{% for message in messages %}
		{% if current_thread.admin %}
			<b>Admin: </b> 
		{% else %}
			<b><a href="/myprofile/{{ message.sender.id }}/">{{ message.sender }}</a>: </b> 
		{% endif %}
		{{ message.content|safe }}<br/>
		<small>Sent on {{ message.date_sent }}</small><br/><br/>	
		{% endfor %}
		{% if not current_thread.admin %}
		<form name="thread" action="/messaging/{{ current_thread.id }}/respond/" method="post">{% csrf_token %}
			<textarea name="content" style="height:100px;width:100%;color:#A6A6A6;" onclick="make_blank_content(this);" onblur="fill_content(this);">Start typing your message here</textarea>
			<input type="image" src="{{ STATIC_URL }}images/send_btn.jpg" alt='Submit Form' style="float:right;">
		</form>
		{% endif %}
{% else %}
	<form name="thread" action="/messaging/startThread/" method="post">{% csrf_token %}
		{% if fail_msg %}
			<label id="fail">{{ fail_msg }}</label>
		{% endif %}
		<input style="width:100%;height:35px;color:#A6A6A6;font-size:15" id="to" type="text" name="usernames" />
		<hr />
		<input type="text" name="subject" value="Subject" onclick="make_blank_subject(this);" onblur="fill_subject(this);" style="width:200px;color:#A6A6A6"/><br/><br/>
		<textarea name="content" style="height:100px;width:100%;color:#A6A6A6;" onclick="make_blank_content(this);" onblur="fill_content(this);">Start typing your message here</textarea><br/>
		{% if friends %}
			<input type="image" src="{{ STATIC_URL }}images/send_btn.jpg" alt='Submit Form' style="float:right;">
		{% else %}
			<br/>You cannot send messages because you have no connections yet
		{% endif %}
	</form>		
{% endif %}
{% endblock %}